package de.bw.jeans.alarminfo.tools;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.ksoap2.transport.KeepAliveHttpTransportSE;

import android.os.Build;
import android.util.Log;

import de.bw.jeans.alarminfo.datatypes.Alarmdaten;

public class LeseIlsAlarme {
	
	private static String AUTH_DATA_READ = AppPorpertiesData.getAppProperties().getProperty("READ_ALERTS_AUTH_DATA_READ");
	
	public static Alarmdaten getAlertById(int alarmId) {
		String METHODEN_NAME = AppPorpertiesData.getAppProperties().getProperty("READ_ALERTS_BYID_METHODEN_NAME");
		String SOAP_ACTION = AppPorpertiesData.getAppProperties().getProperty("READ_ALERTS_BYID_SOAP_ACTION");
		String NAMESPACE = AppPorpertiesData.getAppProperties().getProperty("READ_ALERTS_BYID_NAMESPACE");
		String URL = AppPorpertiesData.getAppProperties().getProperty("READ_ALERTS_BYID_URL");
		
		try {
			SoapObject request = new SoapObject(NAMESPACE, METHODEN_NAME);
			request.addProperty("authData", AUTH_DATA_READ);
			request.addProperty("alert_id", String.valueOf(alarmId));
			
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);
			envelope.setOutputSoapObject(request);
			
			HttpTransportSE httpTransport;
			if(android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD) {
				// Da Markus Geider noch die Version 2.2.2 hat...
				httpTransport = new KeepAliveHttpTransportSE(URL);					
			}
			else {					
				httpTransport = new HttpTransportSE(URL);
			}
			httpTransport.call(SOAP_ACTION, envelope);
			
			SoapObject result = (SoapObject) envelope.getResponse();
						
			return convertResult(result);
		}
		catch (Exception ex) {
			return new Alarmdaten();
		}
	}
	
	public static String[] getLast5AlertIds() {
		String METHODEN_NAME = AppPorpertiesData.getAppProperties().getProperty("READ_ALERTS_LAST5_METHODEN_NAME");
		String SOAP_ACTION = AppPorpertiesData.getAppProperties().getProperty("READ_ALERTS_LAST5_SOAP_ACTION");
		String NAMESPACE = AppPorpertiesData.getAppProperties().getProperty("READ_ALERTS_LAST5_NAMESPACE");
		String URL = AppPorpertiesData.getAppProperties().getProperty("READ_ALERTS_LAST5_URL");
		
		try {
			SoapObject request = new SoapObject(NAMESPACE, METHODEN_NAME);
			request.addProperty("authData", AUTH_DATA_READ);
			
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);
			envelope.setOutputSoapObject(request);
			
			HttpTransportSE httpTransport;
			if(android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD) {
				// Da Markus Geider noch die Version 2.2.2 hat...
				httpTransport = new KeepAliveHttpTransportSE(URL);					
			}
			else {					
				httpTransport = new HttpTransportSE(URL);
			}
			httpTransport.call(SOAP_ACTION, envelope);
			
			String result = envelope.getResponse().toString();
			String[] alarmIds = result.split(",");

			return alarmIds;
		}
		catch (Exception ex) {
			String info = ex.getMessage();
			System.out.println(info);
			return null;
		}
	}
	
	public static Alarmdaten getLastAlert() {
		return getAlertById(0);
	}

	private static Alarmdaten convertResult(SoapObject result) {
		
		Alarmdaten alarmDaten = new Alarmdaten();
		
		try {
		if(result.hasProperty("daten_ok")) {
			if(result.getPropertyAsString("daten_ok").equals("true"))
				alarmDaten.setDatenOk(true);
			else
				alarmDaten.setDatenOk(false);
		}
		if(result.hasProperty("id"))
			alarmDaten.setAlarmId(result.getPropertyAsString("id"));
		if(result.hasProperty("stichwort"))
			alarmDaten.setStichwort(result.getPropertyAsString("stichwort"));
		if(result.hasProperty("patientenname"))
			alarmDaten.setPatientenname(result.getPropertyAsString("patientenname"));
		if(result.hasProperty("einsatz_strasse"))
			alarmDaten.setEinsatzStrasse(result.getPropertyAsString("einsatz_strasse"));
		if(result.hasProperty("einsatz_ort"))
			alarmDaten.setEinsatzOrt(result.getPropertyAsString("einsatz_ort"));
		if(result.hasProperty("geo_longitude"))
			alarmDaten.setLaengengrad(result.getPropertyAsString("geo_longitude"));
		if(result.hasProperty("geo_latitude"))
			alarmDaten.setBreitengrad(result.getPropertyAsString("geo_latitude"));
		if(result.hasProperty("melder_name"))
			alarmDaten.setMelderName(result.getPropertyAsString("melder_name"));
		if(result.hasProperty("melder_zusatz"))
			alarmDaten.setMelderZusatz(result.getPropertyAsString("melder_zusatz"));
		if(result.hasProperty("melder_erreichbarkeit"))
			alarmDaten.setMelderErreichbarkeit(result.getPropertyAsString("melder_erreichbarkeit"));
		if(result.hasProperty("mail_datum"))
			alarmDaten.setMailDatum(result.getPropertyAsString("mail_datum"));
		if(result.hasProperty("mail_uhrzeit"))
			alarmDaten.setMailUhrzeit(result.getPropertyAsString("mail_uhrzeit"));
		if(result.hasProperty("mail_html"))
			alarmDaten.setMailHtml(result.getPropertyAsString("mail_html"));
		
		if(alarmDaten.getAlarmId().equals("anyType{}"))
			alarmDaten.setAlarmId("");
		if(alarmDaten.getStichwort().equals("anyType{}"))
			alarmDaten.setStichwort("");
		if(alarmDaten.getPatientenname().equals("anyType{}"))
			alarmDaten.setPatientenname("");
		if(alarmDaten.getEinsatzStrasse().equals("anyType{}"))
			alarmDaten.setEinsatzStrasse("");
		if(alarmDaten.getEinsatzOrt().equals("anyType{}"))
			alarmDaten.setEinsatzOrt("");
		if(alarmDaten.getLaengengrad().equals("anyType{}"))
			alarmDaten.setLaengengrad("");
		if(alarmDaten.getBreitengrad().equals("anyType{}"))
			alarmDaten.setBreitengrad("");
		if(alarmDaten.getMelderName().equals("anyType{}"))
			alarmDaten.setMelderName("");
		if(alarmDaten.getMelderZusatz().equals("anyType{}"))
			alarmDaten.setMelderZusatz("");
		if(alarmDaten.getMelderErreichbarkeit().equals("anyType{}"))
			alarmDaten.setMelderErreichbarkeit("");
		if(alarmDaten.getMailDatum().equals("anyType{}"))
			alarmDaten.setMailDatum("");
		if(alarmDaten.getMailUhrzeit().equals("anyType{}"))
			alarmDaten.setMailUhrzeit("");
		if(alarmDaten.getMailHtml().equals("anyType{}"))
			alarmDaten.setMailHtml("");
		}
		catch (Exception ex) {
			Log.d("AlarmInfo", "Fehler beim Umwandeln der Alarminfo...");
		}
		
		return alarmDaten;
	}
}
